package com.ophiux.cs.infrastructure.repositories.consultBase;

import com.ophiux.cs.domain.consultBase.model.entity.Dept;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptParentResult;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptQueryObj;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptResult;
import com.ophiux.cs.domain.consultBase.repositories.IDeptRepository;
import com.ophiux.cs.infrastructure.compoment.basedata.mybatis.DataComponent;
import com.ophiux.cs.infrastructure.mapper.consultBase.DeptMapper;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DeptRepository implements IDeptRepository {
	@Autowired
	DeptMapper deptMapper;
	@Autowired
	DataComponent dataComponent;

	@Override
	public void createDept(Dept dept) throws OphiuxRepositoryException {
		try {
			Dept deptParam=new Dept();
			deptParam.setDEPT_NAME(dept.getDEPT_NAME());
			deptParam.setHOSPITAL_ID(dept.getHOSPITAL_ID());
			//根据科室名称医院编码判断重复
			List<Dept> deptList=deptMapper.selectDept(deptParam);
			if(deptList==null || deptList.size()==0){
				deptMapper.addDept(dept);
			}else if(deptList.size()==1){//存在，更新
				dept.setDEPT_ID(deptList.get(0).getDEPT_ID());
				deptMapper.updateDept(dept);
			}else{
				throw new OphiuxRepositoryException("存在多条相同的科室");
			}
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>createDept()异常，",e);
			throw new OphiuxRepositoryException("新增科室异常", e);
		}
	}

	@Override
	public void updateDept(Dept Dept) throws OphiuxRepositoryException {
		try {
			deptMapper.updateDept(Dept);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>updateDept()异常，",e);
			throw new OphiuxRepositoryException("更新医生异常", e);
		}
	}

	@Override
	public void deleteDept(String DeptId) throws OphiuxRepositoryException {
		try {
			deptMapper.deleteDept(DeptId);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>deleteDept()异常，",e);
			throw new OphiuxRepositoryException("删除医生异常", e);
		}
	}

	@Override
	public List<Dept> selectDept() throws OphiuxRepositoryException {
		List<Dept> deptList=null;
		try {
			deptList=deptMapper.selectDept(null);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>selectDept()异常，",e);
			throw new OphiuxRepositoryException("查询医院异常", e);
		}
		return  deptList;
	}

	@Override
	public Dept getDeptByID(String DEPT_ID) throws OphiuxRepositoryException {
		Dept dept=null;
		try {
			dept=deptMapper.getDeptById(DEPT_ID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>getDeptByID()异常，",e);
			throw new OphiuxRepositoryException("查询异常", e);
		}
		return dept;
	}

	@Override
	public List<DeptParentResult> getParentDept(DeptQueryObj dept) throws OphiuxRepositoryException {
		List<DeptParentResult> deptList=null;
		try {
			deptList=deptMapper.getParentDept(dept);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>getParentDept()异常，",e);
			throw new OphiuxRepositoryException("查询科室异常", e);
		}
		return  deptList;
	}
	@Override
	public int getParentDeptCount(DeptQueryObj dept) throws OphiuxRepositoryException {
		int count=0;
		try {
			count=deptMapper.getParentDeptCount(dept);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>getParentDeptCount()异常，",e);
			throw new OphiuxRepositoryException("查询科室异常", e);
		}
		return  count;
	}
	@Override
	public List<DeptResult> getDeptByParentId(String PARENT_DEPT_ID) throws OphiuxRepositoryException{
		List<DeptResult> deptList=null;
		try {
			deptList=deptMapper.getDeptByParentId(PARENT_DEPT_ID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptRepository=>getDeptByParentId()异常，",e);
			throw new OphiuxRepositoryException("查询科室异常", e);
		}
		return  deptList;
	}
}
